<template>
	<view class="home">

		<!-- <update></update> -->

		<!-- 首页顶部搜索区域 -->
		<view class="home-header">
			<view class="header-nav">
				<image src="@/static/image/hot.png"></image>
				<text>公告 : </text>
				<u-notice-bar :text="announcementInfo" direction="row" bgColor="transparent" icon="" color="#000"
					fontSize="26rpx"></u-notice-bar>
			</view>
			<view class="header-search">
				<u-input placeholder="输入医生姓名/医院名称进行搜索" v-model="name" border="surround" fontSize="20rpx" shape="circle">
					<template slot="suffix">
						<u-button type="success" size="mini" shape="circle" color="#54b6ab"
							@click="getInSearchDoctor(name)">搜 索</u-button>
					</template>
				</u-input>
			</view>
		</view>
		<!-- 首页顶部宝宝的候诊室区域 -->
		<view class="home-set">
			<view class="set-title">宝宝的候诊室</view>
			<view class="set-nav">
				<view class="nav-box" v-for="(item,index) in navList" :key="index" @click="getInConsultation(item)">
					<image :src="item.url" mode=""></image>
					<text>{{item.name}}</text>
					<text>( {{index==0?countObj.count1:index==1?countObj.count3:countObj.count2}} )</text>
				</view>
			</view>
		</view>
		<!-- 首页顶部门诊区域 -->
		<view class="home-door">
			<view class="door-box" @click="getInOutpatientService(0)">
				<image src="@/static/image/ordinary.png" mode=""></image>
				<view class="left-box">
					<text style="color: #000;margin-bottom: 5rpx;">普通门诊</text>
					<text>医生急速接诊</text>
				</view>
			</view>
			<view class="door-box" @click="getInOutpatientService(1)">
				<image src="@/static/image/expert.png" mode=""></image>
				<view class="left-box">
					<text style="color: #000;margin-bottom: 5rpx;">专家门诊</text>
					<text>高学历丶经验丰富</text>
				</view>
			</view>
		</view>
		<!-- 首页线上预约区域 -->
		<view class="home-reservation">
			<view class="reservation-title">线上预约</view>
			<!-- 日期选择区域 -->
			<u-scroll-list :indicator="false">
				<view v-for="(item, index) in weekList" :key="index">
					<view class="date-box" :class="dataIndex === index ? 'active' : ''" @click="chooseWeek(index)">
						<text>{{item.dateTime}}</text>
						<text>{{item.weekName}}</text>
					</view>
				</view>
			</u-scroll-list>
			<!-- 列表区域 -->

			<view class="reservation-list">

				<view v-show="doctorList.length==0">
					<u-empty mode="data" icon="http://cdn.uviewui.com/uview/empty/data.png" text="暂无医生排班">
					</u-empty>
				</view>
				
				<view class="list-box" v-for="item in doctorList" :key="item.id" v-show="doctorList.length>0&&flag==false">
					<u-avatar size="50" src="../static/image/doctor.jpg" @click="toDetail(item.id)"></u-avatar>
					<view class="list-count" @click="toDetail(item.id)">
						<text class="title">
							<text style="font-weight: bold;color: #000;">
								{{item.realName}}
							</text>
							<text class="title" style="margin-left: 15rpx;">
								{{item.occupationTitle}}
							</text>
						</text>
						<text class="title" style="color: #000;">{{item.hospital}} | {{item.administrativeOffice}}</text>
						<u--text :lines="2" size="22rpx" :text="'擅长:'+item.personalExpertise"></u--text>
					</view>
					<view class="list-btn">
						<text class="money">¥{{item.consultExpense==0.00?'免费':item.consultExpense}}</text>
						<u-button type="success" size="mini" shape="circle" color="#54b6ab"
							@click="toNav(item.id)">立即预约</u-button>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				name: '',
				announcementInfo: '',
				navList: [{
						name: '问诊中',
						url: require('@/static/image/img1.png'),
						src: '/pages/consultation?type=1',
						num: 1
					},
					{
						name: '咨询记录',
						url: require('@/static/image/img2.png'),
						src: '/pages/consultation?type=2',
						num: 1
					},
					{
						name: '待办事项',
						url: require('@/static/image/img3.png'),
						src: '/pages/toDo',
						num: 3
					}
				],
				dataIndex: 0,
				doctorList: [], //医生列表
				weekList: [],
				countObj:{},
				flag:null,
				userInfo:{},
			}
		},
		watch: {
			"$store.state.websocketData":{
				handler(newVal, oldVal) {
					this.userInfo.notReadCount=this.userInfo.notReadCount+1
					if(this.userInfo.notReadCount==0){
						uni.removeTabBarBadge({
							index:2
						})	
					} else {
						uni.setTabBarBadge({
							index: 2,
							text: this.userInfo.notReadCount+''
						})
					}
				},
			}
			// "$store.state.websocketData": function(val, oldval) {
			// 	this.getInfo()
			// }
		},
		onShow() {
			this.getInfo()
			this.findCount()
			this.findAllAnnouncement()
			this.findCurrentWeekDoctorList()
		},
		methods: {
			//查询问诊中，和待办事项的数量
			async findCount(){
				const{data} = await uni.$http.post('/patient-wait-do/findCount')
				if(data.code===200){
					this.countObj = data.data
				}
			},
			//跳转医生详情页
			toDetail(doctorId) {
				uni.navigateTo({
					url: '/pages/doctorDetails?doctorId=' + doctorId
				})
			},
			//立即预约
			toNav(doctorId) {
				uni.navigateTo({
					url:'/pages/doctorDetails?doctorId='+doctorId
				})
				// uni.setStorageSync('doctorId', doctorId)
				// uni.switchTab({
				// 	url: '/pages/nav'
				// })
			},
			//切换星期
			chooseWeek(index) {
				this.dataIndex = index
				this.doctorList = this.weekList[this.dataIndex].doctorList
				this.flag = this.weekList[this.dataIndex].flag
			},
			//查询公告
			async findAllAnnouncement() {
				const {
					data
				} = await uni.$http.post('/announcement/findAll')
				if (data.code === 200) {
					let list = data.data
					let content
					for (var i = 0; i < list.length; i++) {
						if (i == 0) {
							content = (i + 1) + '.' + list[i].announcementContent
						} else {
							content = content + '&emsp;' + (i + 1) + '.' + list[i].announcementContent
						}
						this.announcementInfo = content
					}
				}
			},
			//查询当前星期医生排班
			async findCurrentWeekDoctorList() {
				const {
					data
				} = await uni.$http.post('/patient-doctor/findCurrentWeekDoctorList')
				if (data.code === 200) {
					this.weekList = data.data
					this.doctorList = this.weekList[this.dataIndex].doctorList
					this.flag = this.weekList[this.dataIndex].flag
				}
			},
			//搜索医生
			getInSearchDoctor(val) {
				if(val){
					uni.navigateTo({
						url: '/pages/searchDoctor?name=' + val
					})
					this.name = ''
				} else {
					uni.showToast({
						icon:'none',
						title:'请输入搜索名称'
					})
				}
				
			},
			//进入问诊
			getInConsultation(item) {
				uni.navigateTo({
					url: item.src
				})
			},
			//进入门诊(普通/专家)
			getInOutpatientService(val) {
				uni.navigateTo({
					url: '/pages/outpatientService?type=' + val
				})
			},
			//获取个人信息
			async getInfo() {
				const {
					data
				} = await uni.$http.post('/user/getUserInfo')
				if (data.code === 200) {
					this.userInfo = data.data
					// this.clientList = this.userInfo.clientList
					if(this.userInfo.notReadCount==0){
						uni.removeTabBarBadge({
							index:2
						})	
					} else {
						uni.setTabBarBadge({
							index: 2,
							text: this.userInfo.notReadCount+''
						})
					}
				}
			},
		},
		beforeDestroy() {
			// this.closeSocket()
		},
	}
</script>

<style lang="scss" scoped>
	.home {
		width: 100%;
		height: 100%;
		background-color: #f2f2f2;
		overflow: auto;

		.home-header {
			width: 100%;
			background-color: #fff;
			padding: 10rpx 2.5%;
			box-sizing: border-box;

			.header-nav {
				width: 100%;
				margin-bottom: 10rpx;
				display: flex;
				align-items: center;

				image {
					width: 40rpx;
					height: 40rpx;
					margin-right: 10rpx;
				}
			}

			.header-search {
				width: 100%;
        /deep/.uni-input-placeholder {
        						font-size: 20rpx;
        					}
			}
		}

		.home-set {
			margin: 20rpx auto 0;
			width: 95%;
			background-color: #fff;
			border-radius: 20rpx;
			padding: 20rpx;
			box-sizing: border-box;

			.set-title {
				color: #555;
				font-weight: bold;
				margin-bottom: 20rpx;
			}

			.set-nav {
				width: 100%;
				display: flex;
				align-items: center;
				justify-content: space-around;

				.nav-box {
					display: flex;
					flex-direction: column;
					align-items: center;
					justify-content: space-around;
					color: #666;

					image {
						width: 100rpx;
						height: 100rpx;
					}

					text {
						margin-top: 10rpx;
					}
				}
			}
		}

		.home-door {
			margin: 20rpx auto 0;
			width: 95%;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.door-box {
				width: 49%;
				padding: 10rpx;
				background-color: #fff;
				border-radius: 20rpx;
				box-sizing: border-box;
				display: flex;
				align-items: center;
				justify-content: space-between;

				image {
					padding: 15rpx;
					box-sizing: border-box;
					width: 100rpx;
					height: 100rpx;
				}

				.left-box {
					width: calc(100% - 100rpx);
					height: 100%;
					display: flex;
					flex-direction: column;
					color: #666;
				}
			}
		}

		.home-reservation {
			margin: 20rpx auto;
			width: 95%;
			background-color: #fff;
			border-radius: 20rpx;
			padding: 20rpx;
			box-sizing: border-box;

			.reservation-title {
				color: #555;
				font-weight: bold;
				margin-bottom: 20rpx;
			}

			.date-box {
				width: 150rpx;
				height: 80rpx;
				display: flex;
				flex-direction: column;
				align-items: center;
			}

			.active {
				color: #54b6ab;
			}

			.reservation-list {
				width: 100%;

				.list-box {
					width: 100%;
					padding: 20rpx;
					box-sizing: border-box;
					border-bottom: 2rpx solid #999;
					display: flex;
					align-items: stretch;
					justify-content: space-between;

					image {
						width: 100rpx;
						height: 100rpx;
					}

					.list-count {
						width: calc(100% - 300rpx);
						display: flex;
						flex-direction: column;

						.title {
							color: #999;
							margin-bottom: 20rpx;
							box-sizing: border-box;
						}

						.count {
							word-break: break-all;
							text-overflow: ellipsis;
							display: -webkit-box;
							-webkit-box-orient: vertical;
							-webkit-line-clamp: 2;
							overflow: hidden;
						}
					}

					.list-btn {
						width: 140rpx;
						display: flex;
						flex-direction: column;
						align-items: flex-end;
						justify-content: space-around;

						.money {
							color: red;
							font-weight: bold;
						}
					}
				}
			}
		}



	}
</style>